package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAccountTraceMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsAccountTrace;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsAccountTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/22 8:35
 */
@Service
public class DtsAccountTraceServiceImpl implements DtsAccountTraceService {
    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Autowired
    private DtsAccountTraceMapper dtsAccountTraceMapper;
    @Override
    public Page<DtsAccountTrace> selectList(String username, String phone, Integer page, Integer limit, String sort, String order, Integer[] statusArray) {
        int[] ints=null;
        if(!StringUtils.isEmpty(username)) {
            List<DtsUser> dtsUserList = dtsUserMapper.selectList(new QueryWrapper<DtsUser>().like(StringUtils.isEmpty(username), "username", username).eq("deleted",false));
        ints = dtsUserList.stream().mapToInt(DtsUser::getId).toArray();

        }
        Page<DtsAccountTrace> dtsUserPage = new Page<>(page, limit);
        QueryWrapper<DtsAccountTrace>  dtsAccountTraceQueryWrapper = new QueryWrapper<>();

        dtsAccountTraceQueryWrapper.orderBy(!StringUtils.isEmpty(sort),order.equals("asc"),sort)
                .in(ints!=null,"user_id",ints)
                .like(!StringUtils.isEmpty(phone),"mobile",phone)
                .in(statusArray!=null,"status",statusArray);
        return dtsAccountTraceMapper.selectPage(dtsUserPage,dtsAccountTraceQueryWrapper);
    }

    @Override
    public void update(DtsAccountTrace dtsAccountTrace) {
        dtsAccountTraceMapper.updateById(dtsAccountTrace);
    }
}
